﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Drawing;

using PercolationTree.Model;
using PercolationTree.Util;

namespace PercolationTree.Experiments
{
    class PercExperiment : IExperiment
    {
        private int m_MaxChildrenCount;
        private int m_MaxLayersCount;
        public PercExperiment(int maxChildrenCount, int maxLayersCount)
        {
            m_MaxChildrenCount = maxChildrenCount;
            m_MaxLayersCount = maxLayersCount;
        }

        public IList<Point> process(FactoryFunction factoryFunc)
        {
            const int kExpCount = 100;

            IList<Point> result = new List<Point>();
            Tree tree = factoryFunc(m_MaxChildrenCount, m_MaxLayersCount);
            while (Statistics.getNodesNumber(tree) < 200)
                tree = factoryFunc(m_MaxChildrenCount, m_MaxLayersCount);

            for (int p = 0; p <= 100; p += 9)
            {
                int resultVal = 0;
                for (int i = 0; i < kExpCount; i++)
                {
                    tree.Percolate(p / 100.0f);
                    resultVal += tree.Layers.Last().Nodes.Where(n => n.Type == NodeType.Invalid).Count();
                }
                resultVal /= kExpCount;
                result.Add(new Point(p, resultVal));
            }
            return result;
        }
    }
}
